package com.weilan.blog.admin.controller;

import com.weilan.blog.admin.service.SysMenuService;
import com.weilan.blog.admin.vo.SysMenuVO;
import com.weilan.blog.base.exception.ThrowableUtils;
import com.weilan.blog.base.validator.group.Delete;
import com.weilan.blog.base.validator.group.GetList;
import com.weilan.blog.base.validator.group.Insert;
import com.weilan.blog.base.validator.group.Update;
import com.weilan.blog.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.weilan.blog.common.global.SysConf;

import com.weilan.blog.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 菜单表
 * @author: 孙 峰
 * @time: 2020/9/28 14:54
 */
@Slf4j
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Autowired
     private SysMenuService sysMenuService;

   @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public String getList(@Validated({GetList.class}) @RequestBody SysMenuVO sysMenuVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.result(SysConf.SUCCESS, sysMenuService.getPageList(sysMenuVO));
    }

    /**
     * 获取所有数据列表
     * @param keyword
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public String getAll(@RequestParam(value = "keyword", required = false) String keyword) {

        return ResultUtil.result(SysConf.SUCCESS, sysMenuService.getAllList(keyword));
    }

    @RequestMapping(value = "/getButtonAll", method = RequestMethod.GET)
    public String getButtonAll(@RequestParam(value = "keyword", required = false) String keyword) {

        return ResultUtil.result(SysConf.SUCCESS, sysMenuService.getButtonAllList(keyword));
    }

    @AvoidRepeatableCommit
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody SysMenuVO sysMenuVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return sysMenuService.addCategoryMenu(sysMenuVO);
    }


    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody SysMenuVO sysMenuVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return sysMenuService.editCategoryMenu(sysMenuVO);
    }


    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody SysMenuVO sysMenuVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return sysMenuService.deleteCategoryMenu(sysMenuVO);
    }

    /**
     * 如果是一级菜单，直接置顶在最前面，二级菜单，就在一级菜单内置顶
     *
     * @author xzx19950624@qq.com
     * @date 2018年11月29日上午9:22:59
     */

    @PostMapping("/stick")
    public String stick(@Validated({Delete.class}) @RequestBody SysMenuVO sysMenuVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return sysMenuService.stickCategoryMenu(sysMenuVO);
    }
}
